﻿/*
 * 设计模式 - 简单工厂，工厂模式，抽象工厂
 * 目的：隐藏创建对象的细节，根据条件创建想要的实例
 */

using System.Data;

var factory = new ERP();
var sql = factory.CreateSqlServer();
using (var con = sql.GetConnection())
{
    con.Open();

    ///
}
var mysql = factory.CreateMySql();
using (var con = mysql.GetConnection())
{
    con.Open();

    ///
}

interface IDAO
{
    IDbConnection GetConnection();
}

class SqlServerDAO : IDAO
{
    public IDbConnection GetConnection()
    {
        throw new NotImplementedException();
    }
}

class MySqlDAO : IDAO
{
    public IDbConnection GetConnection()
    {
        throw new NotImplementedException();
    }
}

static class Factory
{
    public static IDAO Create(string name)
    {
        if (name.Equals("SQL"))
        {
            return new SqlServerDAO();
        }
        if (name.Equals("MySql"))
        {
            return new MySqlDAO();
        }
        throw new NotSupportedException();
    }
}


abstract class AbstractionFactory
{
    public abstract IDAO Create();
}

class SqlServerFactory : AbstractionFactory
{
    public override IDAO Create()
    {
        return new SqlServerDAO();
    }
}

class MySqlFactory : AbstractionFactory
{
    public override IDAO Create()
    {
        return new MySqlDAO();
    }
}


abstract class AbsFactory
{
    public abstract IDAO CreateSqlServer();
    public abstract IDAO CreateMySql();

    // CreatePostgreSql()
}

class ERP : AbsFactory
{
    public override IDAO CreateMySql()
    {
        return new MySqlDAO();
    }

    public override IDAO CreateSqlServer()
    {
        return new SqlServerDAO();
    }
}